package main.Q901_1000;

import java.util.*;

public class Q911_920 {
    public static void main(String[] args) {
        System.out.println("Question911：在线选举");
        System.out.println("Question912：排序数组");
        System.out.println("Question913：");
        System.out.println("Question914：");
        System.out.println("Question915：分割数组");
        System.out.println("Question916：");
        System.out.println("Question917：");
        System.out.println("Question918：环形子数组的最大和");
        System.out.println("Question919：完全二叉树插入器");
        System.out.println("Question920：");
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Question911 {
    List<Integer> tops;
    Map<Integer, Integer> voteCounts;
    int[] times;

    public Question911(int[] persons, int[] times) {
        tops = new ArrayList<Integer>();
        voteCounts = new HashMap<Integer, Integer>();
        voteCounts.put(-1, -1);
        int top = -1;
        for (int i = 0; i < persons.length; ++i) {
            int p = persons[i];
            voteCounts.put(p, voteCounts.getOrDefault(p, 0) + 1);
            if (voteCounts.get(p) >= voteCounts.get(top)) {
                top = p;
            }
            tops.add(top);
        }
        this.times = times;
    }

    public int q(int t) {
        int l = 0, r = times.length - 1;
        while (l < r) {// 找到满足 times[l] <= t 的最大的 l
            int m = l + (r - l + 1) / 2;
            if (times[m] <= t) {
                l = m;
            } else {
                r = m - 1;
            }
        }
        return tops.get(l);
    }
}

class Question912 {
    public int[] sortArray(int[] nums) {
        Arrays.sort(nums);
        return nums;
    }
}

class Question915 {
    public int partitionDisjoint(int[] nums) {
        int n = nums.length;
        int leftMax = nums[0], leftIndex = 0, curMax = nums[0];
        for (int i = 1; i < n - 1; i++) {
            curMax = Math.max(curMax, nums[i]);
            if (nums[i] < leftMax) {
                leftMax = curMax;
                leftIndex = i;
            }
        }
        return leftIndex + 1;
    }
}

class Question918 {
    public static int maxSubarraySumCircular(int[] nums) {
        int length = nums.length, sum = 0, max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
        int[] dpMax = new int[length], dpMin = new int[length];
        for (int i = 0; i < length; i++) {
            if (i == 0) dpMax[i] = nums[i];
            else {
                if (dpMax[i - 1] > 0) dpMax[i] = nums[i] + dpMax[i - 1];
                else dpMax[i] = nums[i];
            }
            sum += nums[i];//总和
            max = Math.max(max, dpMax[i]);//最大序列和
        }
        for (int i = 0; i < length; i++) {//最小序列和
            if (i == 0) dpMin[i] = nums[i];
            else dpMin[i] = nums[i] + Math.min(0, dpMin[i - 1]);
            min = Math.min(min, dpMin[i]);
        }
        if (sum == min && sum < 0) return max;//全为负数时
        else return Math.max(sum - min, max);
    }
}

class Question919 {
    TreeNode root;
    Queue<TreeNode> qu = new LinkedList<>();

    public Question919(TreeNode root) {
        this.root = root;

        qu.offer(root);
        while (qu.peek().left != null && qu.peek().right != null) {
            TreeNode node = qu.poll();
            qu.offer(node.left);
            qu.offer(node.right);
        }
    }

    public int insert(int v) {

        TreeNode father = qu.peek();
        if (father.left == null) {
            father.left = new TreeNode(v);
        } else {
            father.right = new TreeNode(v);
            qu.poll();
            qu.offer(father.left);
            qu.offer(father.right);
        }

        return father.val;
    }

    public TreeNode get_root() {
        return this.root;
    }
}